//
//  DetailPage.m
//  AsyncImages
//
//  Created by Matthew on 11-12-25.
//  Copyright 2011年 develop4u.gr. All rights reserved.
//

#import "DetailPage.h"
#import "ColorUtil.h"
#import "IPhone_IPad_Judge.h"
#import "UIImageLayer.h"

@implementation DetailPage

@synthesize suffix;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        titleText = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, frame.size.width, 45)];
        titleText.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18.0];
        [titleText setBackgroundColor:[UIColor clearColor]];
        [titleText setTextColor:[ColorUtil stringHexToUIColor:@"0x37445f"]];
        titleText.textAlignment = UITextAlignmentLeft;
        [titleText setText:@""];
        
        dateText= [[UILabel alloc] initWithFrame:CGRectMake(20, 27, frame.size.width, 45)];
        dateText.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:15.0];
        [dateText setBackgroundColor:[UIColor clearColor]];
        [dateText setTextColor:[UIColor grayColor]];
        dateText.textAlignment = UITextAlignmentLeft;
        [dateText setText:@""];
        
        infoText= [[UILabel alloc] initWithFrame:CGRectMake(20, 243-45, frame.size.width-20, 100)];
        infoText.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:15.0];
        [infoText setBackgroundColor:[UIColor clearColor]];
        [infoText setTextColor:[UIColor blackColor]];
        infoText.textAlignment = UITextAlignmentLeft;
        infoText.numberOfLines = 0;
        //[infoText sizeToFit];
        [infoText setText:@""];

        detailText= [[UILabel alloc] initWithFrame:CGRectMake(20, 243-45+20, frame.size.width-20, 100)];
        detailText.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:15.0];
        [detailText setBackgroundColor:[UIColor clearColor]];
        [detailText setTextColor:[UIColor blackColor]];
        detailText.textAlignment = UITextAlignmentLeft;
        detailText.numberOfLines = 0;
        //[detailText sizeToFit];
        detailText.hidden = TRUE;
        [detailText setText:@""];
        
        activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activityIndicator.frame = CGRectMake(frame.size.width/2-20, frame.size.height/2-20, 40.0, 40.0);
        [activityIndicator stopAnimating];
        
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320, self.frame.size.height)];
        scrollView.contentSize = CGSizeMake(320,self.frame.size.height);
        scrollView.bounces = FALSE;
        [self addSubview:scrollView];
        //[scrollView setPagingEnabled:YES];
        //[scrollView setShowsHorizontalScrollIndicator:NO];
        
        [scrollView addSubview:activityIndicator];
        [scrollView addSubview:titleText];
        [scrollView addSubview:dateText];
        [scrollView addSubview:infoText];
        [scrollView addSubview:detailText];
        
        segClicked = FALSE;
        seg = [[UISegmentedControl alloc] initWithItems:[[NSArray alloc] initWithObjects:@"查看简介",@"查看评论", nil]];
        seg.frame = CGRectMake(36, 330-45, 250, 40);
        seg.selectedSegmentIndex = -1;
        [seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        //[seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchDown];
        [scrollView addSubview:seg];
        
        imageLoaded = FALSE;
        
        self.backgroundColor = [ColorUtil stringHexToUIColor:@"0xf0f0f0"];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void) triggerByBuildId:(NSString *)inId
{

    [activityIndicator startAnimating];
    if (detailXMLParser != Nil) 
    {
        [detailXMLParser reset];
        [detailXMLParser release];
    }
    
    if (centerImg != Nil)// && imageLoaded) 
    {
        [centerImg reset];
        [centerImg removeFromSuperview];
        [centerImg release];
    }
    
    [titleText setText:@""];
    [dateText setText: @""];
    [infoText setText:@""];

    detailText.hidden = TRUE;
    [seg setTitle:@"查看简介" forSegmentAtIndex:0];
    seg.frame = CGRectMake(36, 330-45, seg.frame.size.width,seg.frame.size.height);
    scrollView.contentSize = CGSizeMake(320,420);
    segClicked = FALSE;

    detailXMLParser = [[MediaBookXMLParser alloc] init];
    detailXMLParser.msgString = load_page_over_msg;
    NSString *url = [[NSString alloc] initWithFormat:@"%@%@",urlPrefix,inId];
    //NSLog(@"the url->%@",url);
    
    imageLoaded = FALSE;
    [detailXMLParser startParsing:url];

    curUrl = [[NSString alloc] initWithString:url];
    
    for (id cell in scrollView.subviews) {
        if ([cell isKindOfClass:[ImageLayerCell class]]) {
            ImageLayerCell * cellView = (ImageLayerCell*)cell;
            [cellView removeFromSuperview];
        }
    }
}

- (void)segmentAction:(UISegmentedControl*)sender 
{

    switch (sender.selectedSegmentIndex) 
    {
        case 0:
            
            if (!segClicked) 
            {
                detailText.hidden = FALSE;
                [seg setTitle:@"收起简介" forSegmentAtIndex:0];
                
                int cellHeight = 160;
                int originY = detailText.frame.origin.y+detailText.frame.size.height+20;
                int i = 0;
                for (SinglePageInfo * singlePageInfo in detailXMLParser.projectsArr) {
                    if (i > 0 && singlePageInfo) {
                        ImageLayerCell * cell = [[ImageLayerCell alloc] initWithFrame:CGRectMake(20, originY, 280, cellHeight)];
                        [cell setImage:singlePageInfo.url];
                        [cell setCountDing:42 cai:3 zhuan:2];
                        [scrollView addSubview:cell];
                        [cell release];
                        
                        originY += cellHeight +5;
                    }
                    ++i;
                }
                originY += 15;

                seg.frame = CGRectMake(36, originY, seg.frame.size.width, seg.frame.size.height);
                scrollView.contentSize = CGSizeMake(320, seg.frame.origin.y + seg.frame.size.height+200);
            }
            else
            {
                for (id cell in scrollView.subviews) {
                    if ([cell isKindOfClass:[ImageLayerCell class]]) {
                        ImageLayerCell * cellView = (ImageLayerCell*)cell;
                        [cellView removeFromSuperview];
                    }
                }
                
                detailText.hidden = TRUE;
                [seg setTitle:@"查看简介" forSegmentAtIndex:0];
                seg.frame = CGRectMake(36, 330-45, seg.frame.size.width,seg.frame.size.height);
                scrollView.contentSize = CGSizeMake(320,420);
            }
            
            segClicked = !segClicked;
            break;
        case 1:
            //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"comment_trigger" object:curUrl];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"comment_trigger"  object:curUrl];

            break;
        case 2:

            break;
        default:
            break;
    }

    seg.selectedSegmentIndex = -1;
}


-(void) setURLPrefixByURL:(NSString *)inURL
{
    urlPrefix = [[NSString alloc] initWithString:inURL];
}

-(void) notificationSelector:(NSNotification *) notification
{
    NSString *curNotificationName = [notification name];
    
    [activityIndicator stopAnimating];
    
    if([curNotificationName isEqualToString:load_page_over_msg])
    {   
        NSLog(@"load page over msg");
        
        /*
        htmlData = [[NSString alloc] initWithFormat: @"<body>"
                    "<font color=\"#37445f\" size=\"15\">%@</font><br/>"
                    "<font color=\"#999999\" size=\"9\">上传日期:%@</font><br/>"
                    //"<font size=\"9\">所属分类：%@</font><br/>"
                    "<font size=\"9\">所在地：%@</font><br/>"
                    //"<font size=\"9\">设计单位：%@</font><br/>"
                    "<br/><font size=\"9\" color=\"#999999\">%@<br/><br/></font>"
                    "</body>",
                    detailXMLParser.propData.name,
                    detailXMLParser.propData.date,
                    detailXMLParser.propData.loc,
                    //detailXMLParser.propData.designer,
                    detailXMLParser.propData.desc];
        */
        
        if ([detailXMLParser.propData.photo length] >0) 
        {
            centerImg = [[UIImageLayer alloc] initWithFrame:CGRectMake(20, 63, 280, 135)];
            [centerImg initImage:detailXMLParser.propData.photo useOriginalSize:FALSE ratioMode:TRUE layerInfo:Nil];
            [scrollView addSubview:centerImg];
        }
        else
        {
            for (int i=0; i< [detailXMLParser.projectsArr count]; i++) 
            {
                SinglePageInfo *singlePageInfo = [detailXMLParser.projectsArr objectAtIndex:i];
                
                if (singlePageInfo.url == Nil) 
                {
                    continue;
                }
                else
                {
                    centerImg = [[UIImageLayer alloc] initWithFrame:CGRectMake(20, 63, 280, 135)];
                    [centerImg initImage:singlePageInfo.url useOriginalSize:FALSE ratioMode:TRUE layerInfo:Nil];
                    [scrollView addSubview:centerImg];
                    break;
                }
            }
        }
        //htmlData = [htmlData stringByAppendingString:@"<br/><br/><br/><br/>"];
        
        [titleText setText:detailXMLParser.propData.name];
        
        if (detailXMLParser.propData.date != Nil) 
        {
            [dateText setText: [@"发布日期：" stringByAppendingString:detailXMLParser.propData.date]];
        }
        
        NSString *infoStr = [[NSString alloc] initWithFormat: @""];
        
        if (detailXMLParser.propData.loc != Nil && [detailXMLParser.propData.loc length]>0) 
        {
            infoStr = [infoStr stringByAppendingFormat:@"所在地：%@ \n",detailXMLParser.propData.loc];
        }
        
        if (detailXMLParser.propData.unit != Nil && [detailXMLParser.propData.unit length]>0) 
        {
            infoStr = [infoStr stringByAppendingFormat:@"设计单位：%@ \n",detailXMLParser.propData.unit];
        }        
        
        [infoText setText:infoStr];
        
        detailText.frame = CGRectMake(detailText.frame.origin.x, 
                                                            infoText.frame.origin.y + infoText.frame.size.height, 
                                                            detailText.frame.size.width, 
                                                            detailText.frame.size.height);
        [detailText setText:detailXMLParser.propData.desc];
        
    }
}


-(void) clean
{
    
}

-(void) configMsg
{
    load_page_over_msg = [[NSString alloc] initWithFormat:@"load_detail_info_over_%@",suffix];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:load_page_over_msg object:Nil];
    
}



-(void) reset
{
    
}

- (void)dealloc
{
    [super dealloc];
}

@end
